package Frame.battleanimation;

import FCRendering.FCRMaster;
import VWorldGameStudio.FCAnimation;
import VWorldGameStudio.FCMonster;

import java.awt.*;

import static FCRendering.FCRMaster.fcv;

//怪物自爆
public class MsZiBaoAni extends ABattleAni{
    //boomItSelfAni
    FCAnimation ani;
    boolean hited = true;//是否撞中
    FCMonster monster;

    int[] endPoint;

    int goFrame = 0;
    int boomFrame = 0;
    int[] startPoint = new int[2];
    int[] paintPoint = new int[2];
    int[] boomPoint = new int[2];
    private int boomPngIndex = -1;
    int endframe = 5;
    Integer[] s;
    private int[] moveEndPoint = new int[2];

    public MsZiBaoAni(FCMonster monster, int[] endPoint){
        this.monster = monster;
        this.endPoint = endPoint;
        ani = FCRMaster.animationLib.getAnimationOfID("boomItSelfAni");

        s = fcv.getBattleMonsterCoor().get(fcv.getBattleMonster().indexOf(monster));
        if (endPoint[0] <= 0 || endPoint[0] >= (int) (1920 * fcv.getProportion())) {
            hited = false;//没打着
        }else {
            moveEndPoint[0] = endPoint[0] - s[2];
            moveEndPoint[1] = endPoint[1] - s[3]/2;
        }
        startPoint[0] = s[0];
        startPoint[1] = s[1];
        //STEP 1计算俩点之间距离，速度假设是v像素/毫秒，
        int v = (int) (700 * fcv.getProportion());
        double targetDistance = AnimationUtil.getDistance(startPoint, endPoint);
        goFrame = (int) (16 * targetDistance / v);

        if (ani.getFirePngUrl().size() > 0) {
            boomFrame = ani.boomFrameTime / 16;
            if (ani.getFirePngUrl().size() > boomFrame) {
                boomFrame = ani.getFirePngUrl().size();
            }
        }
        if(boomFrame < endframe){
            allFrames = goFrame + endframe;
        }else {
            allFrames = boomFrame + goFrame;
        }
    }

    @Override
    public void nextFrame(int frame) {
        if(frame <= goFrame) {
            AnimationUtil.getAnimatePoint(paintPoint, startPoint, moveEndPoint, (float) frame / goFrame);
            s[0] = paintPoint[0];
            s[1] = paintPoint[1];
        }else {

            //爆炸一下
            int p = boomFrame / ani.getFirePngUrl().size();
            int index = (frame - goFrame) / p + 1;
            boomPngIndex = Math.min(index, ani.getFirePngUrl().size() - 1);
            boomPngIndex = Math.max(boomPngIndex, 0);

            boomPoint[0] = s[0]+ s[2] + (int) (ani.firePngCenterOffsetX * fcv.getProportion());
            boomPoint[1] = s[1] +s[3]/2+ (int) (ani.firePngCenterOffsetY * fcv.getProportion());

            if (hited) {//被打中才闪
                int cha = frame - goFrame;
                if (cha < endframe / 4) {
                    beHitShowVisable = false;
                } else if (cha < endframe / 2) {
                    beHitShowVisable = true;
                } else if (cha < 3 * endframe / 4) {
                    beHitShowVisable = false;
                } else {
                    beHitShowVisable = true;
                }
            }
            if(frame == goFrame+4) {
               //TODO 隐藏
                monster.setShowInBattle(false);
            }
            if(frame == allFrames) {
                //TODO 一定要回归原位，因为呼叫同伴有需要用到旧的位置
                s[0] = startPoint[0];
                s[1] = startPoint[1];
            }
        }
    }

    @Override
    public void paintBattleSceen(Graphics g, FCRMaster fcrMaster) {
        if (boomPngIndex != -1) {
            g.drawImage(ani.getFirePng().get(boomPngIndex), boomPoint[0], boomPoint[1],
                    (int) (ani.firePngSizeW * fcv.getProportion()), (int) (ani.firePngSizeH * fcv.getProportion()), fcrMaster);
        }
    }
}
